package proxy
{
    import com.pamakids.models.ResultVO;
    import com.pamakids.services.QNService;
    
    import flash.utils.ByteArray;
    import flash.utils.Dictionary;
    
    import controllers.API;
    
    import models.VO;
    
    public class QNServiceSub extends QNService
    {
        private var callbackDic:Dictionary = new Dictionary();
        
        override public function upload(file:Object, callback:Function, key:Object=null):void
        {
            if(key.hasOwnProperty('key'))
                key = key.key;
            
            var func:Function = functionCreator();
            callbackDic[func] = [file, callback, key, super.upload];
            
            var s:String = key.split('.').pop();
            if( s=='amr' || s == 'mp3' )
                API.instance.getAudioToken(func, key as String);
            else
                API.instance.getToken(func, key as String);
        }
        
        override public function uploadBA(ba:ByteArray, data:Object, cb:Function):void
        {
            var key:String;
            if(data.hasOwnProperty('key'))
                key = data.key;
            else if(data is String)
                key=data as String;
            
            var func:Function = functionCreator2();
            callbackDic[func] = [ba, cb, key, super.uploadBA];
            
            API.instance.getAudioToken(func, key);
        }
        
        public function uploadVideoBA(ba:ByteArray, data:Object, cb:Function):void
        {
            var key:String;
            if(data.hasOwnProperty('key'))
                key = data.key;
            else if(data is String)
                key=data as String;
            
            var func:Function = functionCreator3();
            callbackDic[func] = [ba, cb, key, super.uploadBA];
            
            API.instance.getVideoToken(func, key);
        }
        
        private function functionCreator():Function
        {
            function func(result:ResultVO):void
            {
                var arr:Array = callbackDic[func];
                if(result.status)
                {
                    var token:String = result.results as String;
                    arr[3](arr[0], arr[1], {'key': arr[2], 'token': token});
                }
                else
                {
                    arr[1](result);
                }
                delete callbackDic[func];
            }
            return func;
        }
        private function functionCreator2():Function
        {
            function func(result:ResultVO):void
            {
                var arr:Array = callbackDic[func];
                if(result.status)
                {
                    var token:String = result.results as String;
                    arr[3](arr[0], {'key': arr[2], 'token': token,'filename':VO.AUDIO_NAME_MP3}, arr[1]);
                }
                else
                {
                    arr[1](result);
                }
                delete callbackDic[func];
            }
            return func;
        }
        
        private function functionCreator3():Function
        {
            function func(result:ResultVO):void
            {
                var arr:Array = callbackDic[func];
                if(result.status)
                {
                    var token:String = result.results as String;
                    arr[3](arr[0], {'key': arr[2], 'token': token,'filename':VO.VIDEO_NAME_FLV}, arr[1]);
                }
                else
                {
                    arr[1](result);
                }
                delete callbackDic[func];
            }
            return func;
        }
    }
}

